package org.hzz.application.command;

import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.convention.page.query.FilterCondition;
import io.gitee.pkmer.convention.page.query.SortOrder;
import io.gitee.pkmer.ddd.shared.command.CommandWithMark;
import io.gitee.pkmer.ddd.shared.service.CmdService;
import lombok.Value;
import org.hzz.application.handler.CouponPageQueryCmdHandler;
import org.hzz.promotions.types.enums.coupon.CouponStatus;
import org.hzz.promotions.types.req.CouponPageQuery;
import org.hzz.promotions.types.vo.CouponPageVo;

import java.util.List;

import static io.gitee.pkmer.convention.page.query.FilterCondition.Operation.*;
import static org.hzz.promotions.types.constants.CouponFields.*;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/8/30
 * 优惠券分页查询命令
 * <p>
 * {@link CouponPageQueryCmdHandler}
 */
@Value(staticConstructor = "commandOf")
public class CouponPageQueryCmd implements CommandWithMark {

    CouponPageQuery pageQuery;

    public static final String MARK = CouponPageQueryCmd.class.getName();

    @Override
    public String mark() {
        return MARK;
    }

    public PageResponse<CouponPageVo> executeWithResult(CmdService cmdService) {
        handlePageQuery();
        return cmdService.<PageResponse<CouponPageVo>>handleCommandWithResult(this);
    }


    void handlePageQuery() {
        String name = pageQuery.getName();
        CouponStatus status = pageQuery.getStatus();
        Byte type = pageQuery.getType();

        // 处理过滤条件
        List<FilterCondition> filters = FilterCondition.createCriteria()
                .and(NAME, LIKE, like(name))
                .and(STATUS, EQ, status)
                .and(TYPE, EQ, type)
                .build();
        pageQuery.setFilters(filters);

        pageQuery.setSortOrders(List.of(
                new SortOrder(CREATE_TIME,true)
        ));
    }

    @Override
    public void execute(CmdService cmdService) {
        throw new UnsupportedOperationException("请使用带返回参数的方法");
    }
}
